home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsII / quantizer.c < prev    next >
Text File  |  1992-06-16  |  12KB  |  432 lines

  1. /**********************************************************************
  2.         C Implementation of Wu's Color Quantizer (v. 2)
  3.         (see Graphics Gems vol. II, pp. 126-133)
  4.  
  5. Author:    Xiaolin Wu
  6.     Dept. of Computer Science
  7.     Univ. of Western Ontario
  8.     London, Ontario N6A 5B7
  9.     wu@csd.uwo.ca
  10.  
  11. Algorithm: Greedy orthogonal bipartition of RGB space for variance
  12.        minimization aided by inclusion-exclusion tricks.
  13.        For speed no nearest neighbor search is done. Slightly
  14.        better performance can be expected by more sophisticated
  15.        but more expensive versions.
  16.  
  17. The author thanks Tom Lane at Tom_Lane@G.GP.CS.CMU.EDU for much of
  18. additional documentation and a cure to a previous bug.
  19.  
  20. Free to distribute, comments and suggestions are appreciated.
  21. **********************************************************************/    
  22.  
  23. #include<stdio.h>
  24.  
  25. #define MAXCOLOR    256
  26. #define    RED    2
  27. #define    GREEN    1
  28. #define BLUE    0
  29.  
  30. struct box {
  31.     int r0;             /* min value, exclusive */
  32.     int r1;             /* max value, inclusive */
  33.     int g0;  
  34.     int g1;  
  35.     int b0;  
  36.     int b1;
  37.     int vol;
  38. };
  39.  
  40. /* Histogram is in elements 1..HISTSIZE along each axis,
  41.  * element 0 is for base or marginal value
  42.  * NB: these must start out 0!
  43.  */
  44.  
  45. float        m2[33][33][33];
  46. long int    wt[33][33][33], mr[33][33][33],    mg[33][33][33],    mb[33][33][33];
  47. unsigned char   *Ir, *Ig, *Ib;
  48. int            size; /*image size*/
  49. int        K;    /*color look-up table size*/
  50. unsigned short int *Qadd;
  51.  
  52. void
  53. Hist3d(vwt, vmr, vmg, vmb, m2) 
  54. /* build 3-D color histogram of counts, r/g/b, c^2 */
  55. long int *vwt, *vmr, *vmg, *vmb;
  56. float    *m2;
  57. {
  58. register int ind, r, g, b;
  59. int         inr, ing, inb, table[256];
  60. register long int i;
  61.         
  62.     for(i=0; i<256; ++i) table[i]=i*i;
  63.     Qadd = (unsigned short int *)malloc(sizeof(short int)*size);
  64.     if (Qadd==NULL) {printf("Not enough space\n"); exit(1);}
  65.     for(i=0; i<size; ++i){
  66.         r = Ir[i]; g = Ig[i]; b = Ib[i];
  67.         inr=(r>>3)+1; 
  68.         ing=(g>>3)+1; 
  69.         inb=(b>>3)+1; 
  70.         Qadd[i]=ind=(inr<<10)+(inr<<6)+inr+(ing<<5)+ing+inb;
  71.         /*[inr][ing][inb]*/
  72.         ++vwt[ind];
  73.         vmr[ind] += r;
  74.         vmg[ind] += g;
  75.         vmb[ind] += b;
  76.              m2[ind] += (float)(table[r]+table[g]+table[b]);
  77.     }
  78. }
  79.  
  80. /* At conclusion of the histogram step, we can interpret
  81.  *   wt[r][g][b] = sum over voxel of P(c)
  82.  *   mr[r][g][b] = sum over voxel of r*P(c)  ,  similarly for mg, mb
  83.  *   m2[r][g][b] = sum over voxel of c^2*P(c)
  84.  * Actually each of these should be divided by 'size' to give the usual
  85.  * interpretation of P() as ranging from 0 to 1, but we needn't do that here.
  86.  */
  87.  
  88. /* We now convert histogram into moments so that we can rapidly calculate
  89.  * the sums of the above quantities over any desired box.
  90.  */
  91.  
  92.  
  93. void
  94. M3d(vwt, vmr, vmg, vmb, m2) /* compute cumulative moments. */
  95. long int *vwt, *vmr, *vmg, *vmb;
  96. float    *m2;
  97. {
  98. register unsigned short int ind1, ind2;
  99. register unsigned char i, r, g, b;
  100. long int line, line_r, line_g, line_b,
  101.      area[33], area_r[33], area_g[33], area_b[33];
  102. float    line2, area2[33];
  103.  
  104.     for(r=1; r<=32; ++r){
  105.     for(i=0; i<=32; ++i) 
  106.         area2[i]=area[i]=area_r[i]=area_g[i]=area_b[i]=0;
  107.     for(g=1; g<=32; ++g){
  108.         line2 = line = line_r = line_g = line_b = 0;
  109.         for(b=1; b<=32; ++b){
  110.         ind1 = (r<<10) + (r<<6) + r + (g<<5) + g + b; /* [r][g][b] */
  111.         line += vwt[ind1];
  112.         line_r += vmr[ind1]; 
  113.         line_g += vmg[ind1]; 
  114.         line_b += vmb[ind1];
  115.         line2 += m2[ind1];
  116.         area[b] += line;
  117.         area_r[b] += line_r;
  118.         area_g[b] += line_g;
  119.         area_b[b] += line_b;
  120.         area2[b] += line2;
  121.         ind2 = ind1 - 1089; /* [r-1][g][b] */
  122.         vwt[ind1] = vwt[ind2] + area[b];
  123.         vmr[ind1] = vmr[ind2] + area_r[b];
  124.         vmg[ind1] = vmg[ind2] + area_g[b];
  125.         vmb[ind1] = vmb[ind2] + area_b[b];
  126.         m2[ind1] = m2[ind2] + area2[b];
  127.         }
  128.     }
  129.     }
  130. }
  131.  
  132.  
  133. long int Vol(cube, mmt) 
  134. /* Compute sum over a box of any given statistic */
  135. struct box *cube;
  136. long int mmt[33][33][33];
  137. {
  138.     return( mmt[cube->r1][cube->g1][cube->b1] 
  139.        -mmt[cube->r1][cube->g1][cube->b0]
  140.        -mmt[cube->r1][cube->g0][cube->b1]
  141.        +mmt[cube->r1][cube->g0][cube->b0]
  142.        -mmt[cube->r0][cube->g1][cube->b1]
  143.        +mmt[cube->r0][cube->g1][cube->b0]
  144.        +mmt[cube->r0][cube->g0][cube->b1]
  145.        -mmt[cube->r0][cube->g0][cube->b0] );
  146. }
  147.  
  148. /* The next two routines allow a slightly more efficient calculation
  149.  * of Vol() for a proposed subbox of a given box.  The sum of Top()
  150.  * and Bottom() is the Vol() of a subbox split in the given direction
  151.  * and with the specified new upper bound.
  152.  */
  153.  
  154. long int Bottom(cube, dir, mmt)
  155. /* Compute part of Vol(cube, mmt) that doesn't depend on r1, g1, or b1 */
  156. /* (depending on dir) */
  157. struct box *cube;
  158. unsigned char dir;
  159. long int mmt[33][33][33];
  160. {
  161.     switch(dir){
  162.     case RED:
  163.         return( -mmt[cube->r0][cube->g1][cube->b1]
  164.             +mmt[cube->r0][cube->g1][cube->b0]
  165.             +mmt[cube->r0][cube->g0][cube->b1]
  166.             -mmt[cube->r0][cube->g0][cube->b0] );
  167.         break;
  168.     case GREEN:
  169.         return( -mmt[cube->r1][cube->g0][cube->b1]
  170.             +mmt[cube->r1][cube->g0][cube->b0]
  171.             +mmt[cube->r0][cube->g0][cube->b1]
  172.             -mmt[cube->r0][cube->g0][cube->b0] );
  173.         break;
  174.     case BLUE:
  175.         return( -mmt[cube->r1][cube->g1][cube->b0]
  176.             +mmt[cube->r1][cube->g0][cube->b0]
  177.             +mmt[cube->r0][cube->g1][cube->b0]
  178.             -mmt[cube->r0][cube->g0][cube->b0] );
  179.         break;
  180.     }
  181. }
  182.  
  183.  
  184. long int Top(cube, dir, pos, mmt)
  185. /* Compute remainder of Vol(cube, mmt), substituting pos for */
  186. /* r1, g1, or b1 (depending on dir) */
  187. struct box *cube;
  188. unsigned char dir;
  189. int   pos;
  190. long int mmt[33][33][33];
  191. {
  192.     switch(dir){
  193.     case RED:
  194.         return( mmt[pos][cube->g1][cube->b1] 
  195.            -mmt[pos][cube->g1][cube->b0]
  196.            -mmt[pos][cube->g0][cube->b1]
  197.            +mmt[pos][cube->g0][cube->b0] );
  198.         break;
  199.     case GREEN:
  200.         return( mmt[cube->r1][pos][cube->b1] 
  201.            -mmt[cube->r1][pos][cube->b0]
  202.            -mmt[cube->r0][pos][cube->b1]
  203.            +mmt[cube->r0][pos][cube->b0] );
  204.         break;
  205.     case BLUE:
  206.         return( mmt[cube->r1][cube->g1][pos]
  207.            -mmt[cube->r1][cube->g0][pos]
  208.            -mmt[cube->r0][cube->g1][pos]
  209.            +mmt[cube->r0][cube->g0][pos] );
  210.         break;
  211.     }
  212. }
  213.  
  214.  
  215. float Var(cube)
  216. /* Compute the weighted variance of a box */
  217. /* NB: as with the raw statistics, this is really the variance * size */
  218. struct box *cube;
  219. {
  220. float dr, dg, db, xx;
  221.  
  222.     dr = Vol(cube, mr); 
  223.     dg = Vol(cube, mg); 
  224.     db = Vol(cube, mb);
  225.     xx =  m2[cube->r1][cube->g1][cube->b1] 
  226.      -m2[cube->r1][cube->g1][cube->b0]
  227.      -m2[cube->r1][cube->g0][cube->b1]
  228.      +m2[cube->r1][cube->g0][cube->b0]
  229.      -m2[cube->r0][cube->g1][cube->b1]
  230.      +m2[cube->r0][cube->g1][cube->b0]
  231.      +m2[cube->r0][cube->g0][cube->b1]
  232.      -m2[cube->r0][cube->g0][cube->b0];
  233.  
  234.     return( xx - (dr*dr+dg*dg+db*db)/(float)Vol(cube,wt) );    
  235. }
  236.  
  237. /* We want to minimize the sum of the variances of two subboxes.
  238.  * The sum(c^2) terms can be ignored since their sum over both subboxes
  239.  * is the same (the sum for the whole box) no matter where we split.
  240.  * The remaining terms have a minus sign in the variance formula,
  241.  * so we drop the minus sign and MAXIMIZE the sum of the two terms.
  242.  */
  243.  
  244.  
  245. float Maximize(cube, dir, first, last, cut,
  246.         whole_r, whole_g, whole_b, whole_w)
  247. struct box *cube;
  248. unsigned char dir;
  249. int first, last, *cut;
  250. long int whole_r, whole_g, whole_b, whole_w;
  251. {
  252. register long int half_r, half_g, half_b, half_w;
  253. long int base_r, base_g, base_b, base_w;
  254. register int i;
  255. register float temp, max;
  256.  
  257.     base_r = Bottom(cube, dir, mr);
  258.     base_g = Bottom(cube, dir, mg);
  259.     base_b = Bottom(cube, dir, mb);
  260.     base_w = Bottom(cube, dir, wt);
  261.     max = 0.0;
  262.     *cut = -1;
  263.     for(i=first; i<last; ++i){
  264.     half_r = base_r + Top(cube, dir, i, mr);
  265.     half_g = base_g + Top(cube, dir, i, mg);
  266.     half_b = base_b + Top(cube, dir, i, mb);
  267.     half_w = base_w + Top(cube, dir, i, wt);
  268.         /* now half_x is sum over lower half of box, if split at i */
  269.         if (half_w == 0) {      /* subbox could be empty of pixels! */
  270.           continue;             /* never split into an empty box */
  271.     } else
  272.         temp = ((float)half_r*half_r + (float)half_g*half_g +
  273.                 (float)half_b*half_b)/half_w;
  274.  
  275.     half_r = whole_r - half_r;
  276.     half_g = whole_g - half_g;
  277.     half_b = whole_b - half_b;
  278.     half_w = whole_w - half_w;
  279.         if (half_w == 0) {      /* subbox could be empty of pixels! */
  280.           continue;             /* never split into an empty box */
  281.     } else
  282.         temp += ((float)half_r*half_r + (float)half_g*half_g +
  283.                  (float)half_b*half_b)/half_w;
  284.  
  285.         if (temp > max) {max=temp; *cut=i;}
  286.     }
  287.     return(max);
  288. }
  289.  
  290. int
  291. Cut(set1, set2)
  292. struct box *set1, *set2;
  293. {
  294. unsigned char dir;
  295. int cutr, cutg, cutb;
  296. float maxr, maxg, maxb;
  297. long int whole_r, whole_g, whole_b, whole_w;
  298.  
  299.     whole_r = Vol(set1, mr);
  300.     whole_g = Vol(set1, mg);
  301.     whole_b = Vol(set1, mb);
  302.     whole_w = Vol(set1, wt);
  303.  
  304.     maxr = Maximize(set1, RED, set1->r0+1, set1->r1, &cutr,
  305.             whole_r, whole_g, whole_b, whole_w);
  306.     maxg = Maximize(set1, GREEN, set1->g0+1, set1->g1, &cutg,
  307.             whole_r, whole_g, whole_b, whole_w);
  308.     maxb = Maximize(set1, BLUE, set1->b0+1, set1->b1, &cutb,
  309.             whole_r, whole_g, whole_b, whole_w);
  310.  
  311.     if( (maxr>=maxg)&&(maxr>=maxb) ) {
  312.     dir = RED;
  313.     if (cutr < 0) return 0; /* can't split the box */
  314.     }
  315.     else
  316.     if( (maxg>=maxr)&&(maxg>=maxb) ) 
  317.     dir = GREEN;
  318.     else
  319.     dir = BLUE; 
  320.  
  321.     set2->r1 = set1->r1;
  322.     set2->g1 = set1->g1;
  323.     set2->b1 = set1->b1;
  324.  
  325.     switch (dir){
  326.     case RED:
  327.         set2->r0 = set1->r1 = cutr;
  328.         set2->g0 = set1->g0;
  329.         set2->b0 = set1->b0;
  330.         break;
  331.     case GREEN:
  332.         set2->g0 = set1->g1 = cutg;
  333.         set2->r0 = set1->r0;
  334.         set2->b0 = set1->b0;
  335.         break;
  336.     case BLUE:
  337.         set2->b0 = set1->b1 = cutb;
  338.         set2->r0 = set1->r0;
  339.         set2->g0 = set1->g0;
  340.         break;
  341.     }
  342.     set1->vol=(set1->r1-set1->r0)*(set1->g1-set1->g0)*(set1->b1-set1->b0);
  343.     set2->vol=(set2->r1-set2->r0)*(set2->g1-set2->g0)*(set2->b1-set2->b0);
  344.     return 1;
  345. }
  346.  
  347.  
  348. Mark(cube, label, tag)
  349. struct box *cube;
  350. int label;
  351. unsigned char *tag;
  352. {
  353. register int r, g, b;
  354.  
  355.     for(r=cube->r0+1; r<=cube->r1; ++r)
  356.        for(g=cube->g0+1; g<=cube->g1; ++g)
  357.       for(b=cube->b0+1; b<=cube->b1; ++b)
  358.         tag[(r<<10) + (r<<6) + r + (g<<5) + g + b] = label;
  359. }
  360.  
  361. main()
  362. {
  363. struct box    cube[MAXCOLOR];
  364. unsigned char    *tag;
  365. unsigned char    lut_r[MAXCOLOR], lut_g[MAXCOLOR], lut_b[MAXCOLOR];
  366. int        next;
  367. register long int    i, weight;
  368. register int    k;
  369. float        vv[MAXCOLOR], temp;
  370.  
  371.     /* input R,G,B components into Ir, Ig, Ib;
  372.        set size to width*height */
  373.  
  374.     printf("no. of colors:\n");
  375.     scanf("%d", &K);
  376.  
  377.     Hist3d(wt, mr, mg, mb, m2); printf("Histogram done\n");
  378.     free(Ig); free(Ib); free(Ir);
  379.  
  380.     M3d(wt, mr, mg, mb, m2);    printf("Moments done\n");
  381.  
  382.     cube[0].r0 = cube[0].g0 = cube[0].b0 = 0;
  383.     cube[0].r1 = cube[0].g1 = cube[0].b1 = 32;
  384.     next = 0;
  385.         for(i=1; i<K; ++i){
  386.             if (Cut(&cube[next], &cube[i])) {
  387.               /* volume test ensures we won't try to cut one-cell box */
  388.               vv[next] = (cube[next].vol>1) ? Var(&cube[next]) : 0.0;
  389.               vv[i] = (cube[i].vol>1) ? Var(&cube[i]) : 0.0;
  390.         } else {
  391.               vv[next] = 0.0;   /* don't try to split this box again */
  392.               i--;              /* didn't create box i */
  393.         }
  394.             next = 0; temp = vv[0];
  395.             for(k=1; k<=i; ++k)
  396.                 if (vv[k] > temp) {
  397.                     temp = vv[k]; next = k;
  398.         }
  399.             if (temp <= 0.0) {
  400.               K = i+1;
  401.               fprintf(stderr, "Only got %d boxes\n", K);
  402.               break;
  403.         }
  404.     }
  405.         printf("Partition done\n");
  406.  
  407.     /* the space for array m2 can be freed now */
  408.  
  409.     tag = (unsigned char *)malloc(33*33*33);
  410.     if (tag==NULL) {printf("Not enough space\n"); exit(1);}
  411.     for(k=0; k<K; ++k){
  412.         Mark(&cube[k], k, tag);
  413.         weight = Vol(&cube[k], wt);
  414.         if (weight) {
  415.         lut_r[k] = Vol(&cube[k], mr) / weight;
  416.         lut_g[k] = Vol(&cube[k], mg) / weight;
  417.         lut_b[k] = Vol(&cube[k], mb) / weight;
  418.         }
  419.         else{
  420.           fprintf(stderr, "bogus box %d\n", k);
  421.           lut_r[k] = lut_g[k] = lut_b[k] = 0;        
  422.         }
  423.     }
  424.  
  425.     for(i=0; i<size; ++i) Qadd[i] = tag[Qadd[i]];
  426.     
  427.     /* output lut_r, lut_g, lut_b as color look-up table contents,
  428.        Qadd as the quantized image (array of table addresses). */
  429. }
  430.  
  431.  
  432.